import { _decorator, Component, Node, Label, Sprite, SpriteAtlas, resources, Enum, assetManager, director, AssetManager } from 'cc';

const { ccclass, property } = _decorator;

enum AgeLimit {
    八岁,
    十二岁,
    十六岁
}

@ccclass
export default class HealthAdvicePanel extends Component {
    @property({ type: Enum(AgeLimit), displayName: `适龄`, tooltip: `适龄限制` })
    ageLimit: AgeLimit = AgeLimit.八岁;

    @property({ displayName: `著作权人` })
    company: string = ``;

    @property({ displayName: `登记号` })
    licenseNumber: string = ``;

    ageLimitAtalsPath: string = `Sprites/Common/AgeLimit`;

    static map: Map<string, AssetManager.Bundle> = new Map();

    onLoad() {
        this.Init();

        let count = 0;
        assetManager.loadBundle('Modle', null, (err, bundle) => {
            if (err) console.log(`加载Bundle包出错：${err}`);
            count++;
            loadFinish();
        });

        assetManager.loadBundle('skybox', null, (err, bundle) => {
            if (err) console.log(`加载Bundle包出错：${err}`);
            count++;
            loadFinish();
        });

        assetManager.loadBundle('UI', null, (err, bundle) => {
            if (err) console.log(`加载Bundle包出错：${err}`);
            count++;
            loadFinish();
        });

        let self = this;
        function loadFinish() {
            if (count >= 3) {
                console.log("分包加载完成")
                self.scheduleOnce(() => {
                    director.loadScene("MainMenu");
                }, 2);
            }
        }

        // this.scheduleOnce(() => {
        //     director.loadScene("MainMenu");
        // }, 2)
    }

    Init() {
        this.node.getChildByName(`CompanyLb`).getComponent(Label).string = `${this.company}`;
        this.node.getChildByName(`LicenseLb`).getComponent(Label).string = `${this.licenseNumber}`;

        //适龄提示角标
        let ageLimitSp: Sprite = this.node.getChildByName(`AgeLimitSp`).getComponent(Sprite);
        switch (this.ageLimit) {
            case AgeLimit.八岁:
                resources.load(this.ageLimitAtalsPath, SpriteAtlas, (err, res: SpriteAtlas) => {
                    ageLimitSp.spriteFrame = res.getSpriteFrame(`8`);
                });
                break;
            case AgeLimit.十二岁:
                resources.load(this.ageLimitAtalsPath, SpriteAtlas, (err, res: SpriteAtlas) => {
                    ageLimitSp.spriteFrame = res.getSpriteFrame(`12`);
                });
                break;
            case AgeLimit.十六岁:
                resources.load(this.ageLimitAtalsPath, SpriteAtlas, (err, res: SpriteAtlas) => {
                    ageLimitSp.spriteFrame = res.getSpriteFrame(`16`);
                });
                break;
        }
    }

}
